//
//  PicViewController.m
//  Weibo
//
//  Created by wpapple on 16/6/7.
//  Copyright © 2016年 红红火火恍恍惚惚嘿嘿嘿. All rights reserved.
//

#import "PicViewController.h"
#import "PicCollectionView.h"
#import "PicViewCollectionViewCell.h"

@interface PicViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@property (strong , nonatomic) UIImagePickerController * imagePickerController;
@property (weak, nonatomic) IBOutlet PicCollectionView *picCollectionView;

@property (strong , nonatomic) PicViewCollectionViewCell * cell;

@end

@implementation PicViewController

-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return self.dataArray.count / 3 + 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (section < self.dataArray.count / 3) {
        return 3;
    }else if (self.dataArray.count % 3 == 3) {
        return 3;
    }
    return self.dataArray.count % 3 + 1;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    PicViewCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    [cell.PicViewButton setImage:[UIImage imageNamed:@"compose_pic_add"] forState:UIControlStateNormal];
    
    cell.PicViewButton.userInteractionEnabled = YES;
    
    cell.DeletePicButton.hidden = YES;
    
//    NSLog(@"%ld",indexPath.section * 3 + indexPath.row + 1);
    
    if (self.dataArray.count >= indexPath.section * 3 + indexPath.row + 1 && self.dataArray.count > 0) {
        
        NSInteger i = indexPath.section * 3 + indexPath.row;
        
//        NSLog(@"%ld",i);
        
        cell.DeletePicButton.tag = i + 100;
        
        cell.DeletePicButton.hidden = NO;
        
        [cell.DeletePicButton addTarget:self action:@selector(touchDeleteButton:) forControlEvents:UIControlEventTouchUpInside];
        
        [cell.PicViewButton setImage:self.dataArray[i] forState:UIControlStateNormal];
        
        cell.PicViewButton.userInteractionEnabled = NO;
        
    }
    
    return cell;
}

- (IBAction)picSelect:(UIButton *)sender {
    
    UIImagePickerController * imagePickerController = [[UIImagePickerController alloc]init];
    
    imagePickerController.delegate = self;
    
    self.imagePickerController = imagePickerController;
    
    [self presentViewController:imagePickerController animated:YES completion:nil];
    
    
    
}


- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(nullable NSDictionary<NSString *,id> *)editingInfo NS_DEPRECATED_IOS(2_0, 3_0){
    
    [self.dataArray addObject:image];
    
    [self.picCollectionView reloadData];
    
    [self.imagePickerController dismissViewControllerAnimated:YES completion:nil];
    
}

-(void)touchDeleteButton:(UIButton *)sender{
    
    NSInteger index = sender.tag - 100;
    
    [self.dataArray removeObjectAtIndex:index];
    
    sender.hidden = YES;
    
    [self.picCollectionView reloadData];
    
}



@end
